package com.orders.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.orders.pojo.Customer;
import com.orders.pojo.Customer_Goods;

public class Customer_GoodsOperation implements Operation {
	private Connection conn;
	private PreparedStatement ps;
	private Customer_Goods cg;
	private ResultSet rs;
	@Override
	public boolean insert(Object obj) {
		try {
			if(obj instanceof Customer_Goods){
				cg = (Customer_Goods)obj;
				conn = ConnectionFactory.getConnection();
				ps = conn.prepareStatement("insert into customer_goods values (?,?,?,?,?)");
				ps.setInt(1, cg.getId());
				ps.setInt(2, cg.getGoods_amount());
				ps.setInt(3, cg.getGoods().getId());
				ps.setInt(4, cg.getCustomer().getId());
				ps.setBoolean(5, cg.isVisible());
				return !ps.execute();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	     return false;
	}

	@Override
	public boolean update(Object obj) {
		try {
			if(obj instanceof Customer_Goods){
				cg = (Customer_Goods)obj;
				conn = ConnectionFactory.getConnection();
				ps = conn.prepareStatement("update customer_goods set goods_amount = ?,goods_id = ?,customer_id = ? visible = ? where id = ?");
				ps.setInt(1, cg.getGoods_amount());
				ps.setInt(2, cg.getGoods().getId());
				ps.setInt(3, cg.getCustomer().getId());
				ps.setBoolean(4, cg.isVisible());
				ps.setInt(5, cg.getId());
				return !ps.execute();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	     return false;
	}

	@Override
	public List select(Object obj) {
		List list = new ArrayList<>();
		CustomerOperation co = new CustomerOperation();
		GoodsOperation go = new GoodsOperation();
		try {
			if(obj instanceof Customer_Goods){
				cg = (Customer_Goods)obj;
				conn = ConnectionFactory.getConnection();
				ps = conn.prepareStatement("select * from customer_goods where id = ?");
				ps.setInt(1, cg.getId());
				rs = ps.executeQuery();
				while(rs.next()){
					cg.setId(rs.getInt("id"));
					cg.setGoods_amount(rs.getInt("goods_amount"));
					cg.setCustomer(co.selectById(rs.getInt("customer_id")));
					cg.setGoods(go.selectById(rs.getInt("goods_id")));
					list.add(cg);
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	     return list;
	}
	
	public Customer_Goods selectById(int id) {
		CustomerOperation co = new CustomerOperation();
		GoodsOperation go = new GoodsOperation();
		cg = new Customer_Goods();
		try {
				conn = ConnectionFactory.getConnection();
				ps = conn.prepareStatement("select * from customer_goods where id = ?");
				ps.setInt(1, cg.getId());
				rs = ps.executeQuery();
				rs.next();
					cg.setId(rs.getInt("id"));
					cg.setGoods_amount(rs.getInt("goods_amount"));
					cg.setCustomer(co.selectById(rs.getInt("customer_id")));
					cg.setGoods(go.selectById(rs.getInt("goods_id")));
				
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	     return cg;
	}

	@Override
	public boolean delete(Object obj) {
		try {
			if(obj instanceof Customer_Goods){
				cg = (Customer_Goods)obj;
				conn = ConnectionFactory.getConnection();
				ps = conn.prepareStatement("update customer_goods set visible = ? where id = ?");
				ps.setBoolean(1, false);
				ps.setInt(2, cg.getId());
				return !ps.execute();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	     return false;
	}

}
